package com.zzyl.controller.security;

import com.zzyl.base.PageBean;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户分页查询
     */
    @ApiOperation("用户分页")
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult pageQuery(@PathVariable Integer pageNum,
                                    @PathVariable Integer pageSize,
                                    @RequestBody UserDto dto){
        PageBean<UserVo> pageBean = userService.pageQuery(pageNum,pageSize,dto);
        return ResponseResult.success(pageBean);
    }

    /**
     * 用户添加
     * @param dto
     * @return
     */
    @ApiOperation("用户添加")
    @PutMapping()
    public ResponseResult add(@RequestBody UserDto dto){
        userService.add(dto);
        return ResponseResult.success();
    }

    @ApiOperation("用户修改")
    @PatchMapping()
    public ResponseResult updateUser(@RequestBody UserDto dto){
        userService.updateUser(dto);
        return ResponseResult.success();
    }


    @ApiOperation("删除用户")
    @DeleteMapping("/remove/{userId}")
    public ResponseResult deleteUserById(@PathVariable Long userId){
        userService.deleteUserById(userId);
        return ResponseResult.success();
    }

    @ApiOperation("启用或禁用用户")
    @PutMapping("/is-enable/{id}/{status}")
    public ResponseResult enableOrDisable(@PathVariable Long id,
                                          @PathVariable String status){
        userService.enableOrDisable(id,status);
        return ResponseResult.success();
    }


    @ApiOperation("用户列表")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody UserDto dto){
        List<UserVo> list = userService.list(dto);
        return ResponseResult.success(list);
    }


    @ApiOperation("密码重置")
    @PostMapping("/reset-passwords/{userId}")
    public ResponseResult updatePasswords(@PathVariable Long userId){
        userService.updatePasswords(userId);
        return ResponseResult.success();
    }


    @ApiOperation("用户查询回显")
    @GetMapping("/current-user")
    public ResponseResult getById(){
        UserVo userVo = userService.getById();
        return ResponseResult.success(userVo);
    }
}
